package com.yanqu.road.dataHelper;

import com.yanqu.road.dataHelper.del.mgr.CrossBattleDelMgr;
import com.yanqu.road.dataHelper.del.mgr.CrossDelMgr;
import com.yanqu.road.dataHelper.del.mgr.GameDelMgr;
import com.yanqu.road.dataHelper.del.mgr.TimerDelMgr;
import com.yanqu.road.dataHelper.manager.*;
import com.yanqu.road.dataHelper.repair.mgr.RepairMainTaskMgr;
import com.yanqu.road.utils.executor.ScheduledThreadPoolHelper;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;

public class DelScript {

    // 修复的类名
    static String repairClassName;
    // 服务器列表
    static String whereStr;
    static List<Long> serverList;
    static List<BaseMgr> mgrList = new ArrayList<>();
    protected static ScheduledExecutorService timerScheduledExecutorService;


    public static String getRepairClassName() {
        return repairClassName;
    }

    public static String getWhereStr() {
        return whereStr;
    }

    public static List<Long> getServerList() {
        return serverList;
    }

    public static final String CONFIG_DIR = System.getProperty("user.dir") + File.separator + "Config" + File.separator;

    //参数1调用的方法,参数2为区服
    public static void main(String[] args){
        try {
            repairClassName = args[0];
            whereStr = args[1];
//            serverStr = args[1];
//            serverList = StringUtils.stringToLongList(serverStr,",");
        }catch (Exception e){
            e.printStackTrace();
            return;
        }
        String dirPath = System.getProperty("user.dir") + File.separator;
        DelScript dbScript = new DelScript();
        if(dbScript.run(dirPath)){
            System.err.println("execute over");
            System.exit(1);
        }else{
            System.err.println("game over");
            System.exit(0);
        }
    }
    private static void initTimerExecutor() {
        int corePoolSize = Runtime.getRuntime().availableProcessors();
        corePoolSize = Math.min(4, Math.max(2, corePoolSize));
        timerScheduledExecutorService = ScheduledThreadPoolHelper.createGameScheduledThreadPoolExecutor(corePoolSize, "timer-gameServer");
    }

    public static ScheduledExecutorService getTimerExecutorService() {
        return timerScheduledExecutorService;
    }


    private boolean run(String dirPath){
        if (!DbMgr.initDB(dirPath)) {
            return false;
        }
        if (!GameDelMgr.init(dirPath)) {
            return false;
        }
        if (!CrossDelMgr.init(dirPath)) {
            return false;
        }
        if (!CrossBattleDelMgr.init(dirPath)) {
            return false;
        }

        initMgr();

        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            System.err.println("DelScript 关闭 ");
        }));


        initTimerExecutor();
        TimerDelMgr.initGlobalTimers();
        while (true){

        }
    }

    private void initMgr(){
        mgrList.add(new GameDelMgr());
        mgrList.add(new CrossDelMgr());
        mgrList.add(new ServerMgr());
        BaseMgr.setMgrList(mgrList);
        new RepairMainTaskMgr();
    }

    public static  <T> T getMgr(Class<T> T){
        for(BaseMgr baseMgr : mgrList){
            if(baseMgr.getClass() == T){
                return (T)baseMgr;
            }
        }
        return null;
    }
}
